package com.neuedu.ex;

import com.neuedu.utils.InputUtils;

import java.util.concurrent.ExecutionException;

/**
 * @author 金山
 * 项目：javase
 * site: https://blog.fulfill.com.cn
 * 描述
 * @data 2024/10/2113:35
 */
public class MulpartException {


    public static void main(String[] args) throws Exception {
        // invokeFunction1();
        // invokeFunction2();

        //演示finally
        // jdbc();


        //finally 和return 语句的顺序
        // int result = div();
        //
        // System.out.println("result = " + result);

        sum(110,120);


    }




    static void invokeFunction1() {
        int flag = 1;
        try{
            switch (flag){
                case 0:
                    String str = "abcdef";
                    str = null;

                    System.out.println("str.length() = " + str.length());
                    break;
                case 1:
                   int result  = 0/0;
                    break;
            }




        }catch (NullPointerException e ){
            System.out.println("产生空指针: "+e.getMessage());

            // System.out.println(e.getStackTrace());

            // System.out.println(e.getStackTrace());
            //
            // System.err.println(e.getStackTrace());
            e.printStackTrace(System.out);
            // e.printStackTrace();
        }catch (ArithmeticException e ){
            System.out.println("算数异常: "+e.getMessage());
            //e.printStackTrace();
        }catch (Exception e ){  //所有异常的父类
            System.out.println("除了算数异常 和空指针之外的 所有 Exception: "+e.getMessage());
            //e.printStackTrace();
        }


    }


    static void invokeFunction2() {
        int flag = 1;
        try{
            switch (flag){
                case 0:
                    String str = "abcdef";
                    str = null;

                    System.out.println("str.length() = " + str.length());
                    break;
                case 1:
                    int result  = 0/0;
                    break;
            }

            //jdk1.7+
        }catch (NullPointerException| ArithmeticException  e ){
            System.out.println("出现异常了，走catch快: ");
        }


    }


    static void  jdbc(){
        //
       try{
           System.out.println("打开链接");


           //操作数据
           System.out.println("操作数据");
           int result = 10/2;   // 0/0 产生异常，
           //    关闭数据库

       }catch (Exception e){
           //异常的情况
           System.out.println("操作数据过程产生异常了....");

       }finally {
           System.out.println("关闭数据库");
       }

    }

    static int div(){
        int numa = InputUtils.getNum("请输入一个数字");
        int numb = InputUtils.getNum("请输入另一个数字");



        try{
            int result  =   numa / numb;

            System.out.println("inner result: "+result);
            return result;
        }catch (Exception e){
            System.out.println("计算出错了");
        }finally {
            return 100;
        }

    }


    /**
     * 计算两个数的加和
     * @param numa    0---100
     * @param numb
     * @return
     */
    static int sum(int numa ,int numb) throws  Exception{
        if(numa>100 || numa<0   || numb>100 || numb<0    ){


            //主动 产生一个异常
            throw   new Exception(String.format("numa和numb只能是 0---100之间的数字 , numa :%d ,numb:%d",numa,numb));

            // try{
           //     throw  new Exception("numa和numb只能是 0---100之间的数字 , numa :%d ,numb:%d".formatted(numa,numb));
           // }catch (Exception e){
           //
           // }

        }

        return numa + numb;
    }












}
